package com.ruoyi.web.controller.system;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysRural;
import com.ruoyi.system.service.ISysRuralService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * 农合机构管理Controller
 *
 * @author moweiyu
 * @date 2021-04-28
 */
@Controller
@RequestMapping("/system/rural")
public class SysRuralController extends BaseController
{
    private String prefix = "system/rural";

    @Autowired
    private ISysRuralService sysRuralService;

    @RequiresPermissions("system:rural:view")
    @GetMapping()
    public String rural()
    {
        return prefix + "/rural";
    }

    /**
     * 查询农合机构管理列表
     */
    @RequiresPermissions("system:rural:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysRural sysRural)
    {
        startPage();
        List<SysRural> list = sysRuralService.selectSysRuralList(sysRural);
        return getDataTable(list);
    }

    /**
     * 通过 ruralName 查询行政区域 Ajax （新增页面）
     */
    @RequiresPermissions("system:rural:list")
    @PostMapping("/listAjax")
    @ResponseBody
    public void listAjax(HttpServletResponse response, SysRural sysRural) throws IOException {

        List<SysRural> list = sysRuralService.selectSysRural(sysRural);

        String data = null;
        if (list.size() != 0) {
            data = "{\"res\":\"fail\"}";
        } else {
            data = "{\"res\":\"success\"}";
        }

        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json; charset=utf-8");
        response.getWriter().write(data);
    }

    /**
     * 通过 ruralName 查询行政区域 Ajax （修改页面）
     */
    @RequiresPermissions("system:rural:list")
    @PostMapping("/listAjaxUpdate")
    @ResponseBody
    public void listAjaxUpdate(HttpServletResponse response, String originRuralName, SysRural sysRural) throws IOException {

        System.out.println("originRuralName--"+originRuralName);

        List<SysRural> list = sysRuralService.selectSysRural(sysRural);

        System.out.println(list.get(0).getRuralName());

        String data = null;
        if (list.size()!=0 && !list.get(0).getRuralName().equals(originRuralName)) {
            data = "{\"res\":\"fail\"}";
        } else {
            data = "{\"res\":\"success\"}";
        }

        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json; charset=utf-8");
        response.getWriter().write(data);
    }

    /**
     * 导出农合机构管理列表
     */
    @RequiresPermissions("system:rural:export")
    @Log(title = "农合机构管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysRural sysRural)
    {
        List<SysRural> list = sysRuralService.selectSysRuralList(sysRural);
        ExcelUtil<SysRural> util = new ExcelUtil<SysRural>(SysRural.class);
        return util.exportExcel(list, "农合机构管理数据");
    }

    /**
     * 新增农合机构管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存农合机构管理
     */
    @RequiresPermissions("system:rural:add")
    @Log(title = "农合机构管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysRural sysRural)
    {
        return toAjax(sysRuralService.insertSysRural(sysRural));
    }

    /**
     * 修改农合机构管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysRural sysRural = sysRuralService.selectSysRuralById(id);
        mmap.put("sysRural", sysRural);
        return prefix + "/edit";
    }

    /**
     * 修改保存农合机构管理
     */
    @RequiresPermissions("system:rural:edit")
    @Log(title = "农合机构管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysRural sysRural)
    {
        return toAjax(sysRuralService.updateSysRural(sysRural));
    }

    /**
     * 删除农合机构管理
     */
    @RequiresPermissions("system:rural:remove")
    @Log(title = "农合机构管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysRuralService.deleteSysRuralByIds(ids));
    }
}
